# Maintainer: Alexey Pavlov <alexpux@gmail.com>

# Before building package need CD into directory with drive letter, like
# cd /c/build/mingw-w64-ruby

_realname=ruby
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.0
pkgrel=1
pkgdesc="An object-oriented language for quick and easy programming (mingw-w64)"
arch=('any')
url="https://www.ruby-lang.org/en"
license=("BSD, custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-libyaml"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-tk")
options=('staticlibs' 'strip')
source=("https://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/${_realname}-${pkgver}.tar.bz2"
        0001-mingw-w64-time-functions.patch
        0002-use-gnu-printf.patch
        0003-fix-check-types.patch
        0004-rbinstall-destdir.patch)
sha256sums=('ec7579eaba2e4c402a089dbc86c98e5f1f62507880fd800b9b34ca30166bfa5e'
            '329994d3bf7e692e18c1faffbedfbd076e5d00257b2100387a773b59b81ccd4e'
            '578bd0830fe96efc7656c732ec46b0658fc436a7a30d8945cf3b8240797809f0'
            '846d73ad389249c54ff4e009ad2fd4cdbc46714fce25c724bc706b3ec2f2fe3a'
            '02382ec3b9e42d7dbb58edad3e41c361d98871711bb2f0320082c2acc6a82e2e')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/0001-mingw-w64-time-functions.patch
  patch -p1 -i ${srcdir}/0002-use-gnu-printf.patch
  patch -p1 -i ${srcdir}/0003-fix-check-types.patch
  patch -p1 -i ${srcdir}/0004-rbinstall-destdir.patch

  autoreconf -fi
}

build() {

  local FFI_INC=$(pkg-config --cflags libffi)
  CPPFLAGS+=" -DFD_SETSIZE=2048 ${FFI_INC} -I${MINGW_PREFIX}/include/ncurses"
  CFLAGS+=" -I${MINGW_PREFIX}/include/ncurses ${FFI_INC}"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/ncurses ${FFI_INC}"

  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -f GNUMakefile DESTDIR="${pkgdir}" install-nodoc
  for script in {erb,gem,irb,rdoc,ri}; do
    install ${srcdir}/ruby-${pkgver}/bin/${script} \
      ${pkgdir}${MINGW_PREFIX}/bin/
  done
}
